<template>
  <div class="C_CSSZ_ZDLXWH">
    <div class="title">字典类型维护</div>
    <div class="query">
      创建日期：
      <el-date-picker
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
        size="small"
        v-model="time"
        @change="time_change"
      >
      </el-date-picker>
      类型编码：
      <el-input
        v-model="query.filter.typeCode"
        size="small"
        class="input_style"
      ></el-input>
      类型名称：
      <el-input
        v-model="query.filter.typeName"
        size="small"
        class="input_style"
      ></el-input>
      <el-button
        size="small"
        icon="el-icon-search"
        type="primary"
        style="margin-left: 5px"
        @click="initTable(true)"
        >搜索</el-button
      >
      <el-button
        size="small"
        icon="el-icon-plus"
        type="primary"
        style="margin-left: 5px"
        @click="open('add')"
        >新建</el-button
      >
    </div>
    <div class="main">
      <el-table
        :data="tableData"
        border
        height="700"
        style="width: 100%"
        :header-cell-style="{
          background: 'rgb(246,250,253)',
          color: '#000000',
        }"
      >
        <el-table-column prop="typecode" label="字典类型编码" width="160">
        </el-table-column>
        <el-table-column prop="typename" label="字典类型名称" width="180">
        </el-table-column>
        <el-table-column prop="describe" label="描述"> </el-table-column>
        <el-table-column prop="createDate" label="创建日期" width="160">
        </el-table-column>
        <el-table-column prop="name" label="状态" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              type="success"
              effect="dark"
              size="mini"
              v-if="!scope.row.delFlag"
            >
              开启
            </el-tag>
            <el-tag
              type="danger"
              effect="dark"
              size="mini"
              v-if="scope.row.delFlag"
            >
              停用
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="!scope.row.delFlag"
              type="text"
              icon="el-icon-remove-outline"
              title="停用"
              @click="open('stop', scope.row)"
            ></el-button>
            <el-button
              type="text"
              v-if="scope.row.delFlag"
              icon="el-icon-circle-check"
              title="启用"
              @click="open('start', scope.row)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              title="编辑"
              @click="open('edit', scope.row)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              title="删除"
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-setting"
              title="字典数据设置"
              @click="ZD_seting(scope.row.typecode)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="text-align: center; margin-top: 5px">
        <el-pagination
          layout="prev, pager, next"
          :page-size="query.pageSize"
          :current-page="query.currentPage"
          :total="total"
          @current-change="page_change"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 弹层 -->
    <el-dialog
      :title="titleText"
      :visible.sync="dialogVisible"
      width="55%"
      :before-close="handleClose"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="right"
        label-width="150px"
        :model="formLabelAlign"
      >
        <el-form-item label="字典类型编码">
          <el-input v-model="formLabelAlign.typecode"></el-input>
        </el-form-item>
        <el-form-item label="字典类型名称">
          <el-input v-model="formLabelAlign.typename"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="formLabelAlign.describe"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="BTN_OK1()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="字典数据设置"
      :visible.sync="dialogVisible_a"
      width="55%"
      :before-close="handleClose_a"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <div style="text-align: right">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-plus"
          @click="addPlus"
          >添加行</el-button
        >
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-minus"
          @click="del_row"
          >删除行</el-button
        >
      </div>
      <el-table
        :data="tableData_a"
        border
        height="400"
        style="width: 100%"
        :header-cell-style="{
          background: 'rgb(246,250,253)',
          color: '#000000',
        }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="name" label="字段内容" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="dictcode"
          label="代码"
          align="center"
          width="220"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.dictcode"></el-input>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="address"
          label="接口代码"
          align="center"
          width="160"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.address"></el-input>
          </template>
        </el-table-column> -->
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose_a">取 消</el-button>
        <el-button type="primary" @click="BTN_OK2">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: "",
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      tableData: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      formLabelAlign: {},
      dialogVisible: false,
      dialogVisible_a: false,
      titleText: "",
      tableData_a: [],
      codes: [],
      time: [],
      query: {
        currentPage: 1,
        pageSize: 15,
        filter: {
          key: "",
          withDisable: true,
          typeCode: "",
          typeName: "",
          startTime: "",
          endTime: "",
        },
      },
      total: 0,
      PCode: "",
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {},
  /**
   * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
   */
  mounted() {
    this.initTable();
  },
  // 组件方法
  methods: {
    open(flag, val) {
      if (flag == "add") {
        this.titleText = "新增字典类型维护";
      }
      if (flag == "edit") {
        this.titleText = "编辑字典类型维护";
        this.formLabelAlign = val;
      }
      if (flag == "stop") {
        this.formLabelAlign = val;
        this.formLabelAlign.delFlag = true;
        this.$put("/Aers/ZdDicttype/Update", this.formLabelAlign);
        this.initTable();
        this.$message.success(
          "操作成功，状态不会马上改变，请手动刷新列表后查看！"
        );
        return;
      }
      if (flag == "start") {
        this.formLabelAlign = val;
        this.formLabelAlign.delFlag = false;
        this.$put("/Aers/ZdDicttype/Update", this.formLabelAlign);
        setTimeout((_) => {
          this.initTable();
        }, 1000);
        this.$message.success(
          "操作成功，状态不会马上改变，请手动刷新列表后查看！"
        );
        return;
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleClose_a() {
      this.dialogVisible_a = false;
    },
    handleSelectionChange(val) {
      this.codes = val;
    },
    addPlus() {
      this.tableData_a.push({ name: "", dictcode: "" });
    },
    del_row() {
      var that = this;
      this.codes.forEach((item) => {
        that.tableData_a.forEach((item2, index) => {
          if (item.dictcode == item2.dictcode) {
            this.tableData_a.splice(index, 1);
          }
        });
      });
    },
    time_change() {
      this.query.filter.startTime = this.time[0];
      this.query.filter.endTime = this.time[1];
      this.initTable();
    },
    initTable(flag) {
      if (flag) {
        this.query.currentPage = 1;
      }
      var body = this.query;
      this.$post("/Aers/ZdDicttype/GetList", body).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    page_change(val) {
      this.query.currentPage = val;
      this.initTable();
    },
    BTN_OK1() {
      var body = this.formLabelAlign;
      if (this.formLabelAlign.id) {
        this.$put("/Aers/ZdDicttype/Update", body).then((res) => {
          if (res.code == 200) {
            this.handleClose();
            this.$message.success("操作成功");
          }
        });
      } else {
        this.$post("/Aers/ZdDicttype/Create", body).then((res) => {
          if (res.code == 200) {
            this.handleClose();
            this.$message.success("操作成功");
          }
        });
      }
    },
    del(id) {
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$deleteF("/Aers/ZdDicttype/Delete?id=" + id).then((res) => {
            if (res.code == 200) {
              this.initTable();
              this.$message.success("删除成功");
            }
          });
        })
        .catch(() => {});
    },
    ZD_seting(code) {
      this.PCode = code;
      this.$post("/Aers/ZdDict/GetAll", { typeCode: code }).then((res) => {
        if (res.code == 200) {
          this.tableData_a = res.data;
          this.dialogVisible_a = true;
        }
      });
    },
    BTN_OK2() {
      var body = {
        typeCode: this.PCode,
        list: this.tableData_a,
      };
      this.$post("/Aers/ZdDict/SetDict", body).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功！");
          this.handleClose_a();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.C_CSSZ_ZDLXWH {
  .title {
    background-color: rgb(233, 242, 255);
    line-height: 45px;
    font-size: 18px;
    font-weight: 700;
    padding: 0 10px;
  }
  .query {
    margin: 0 10px;
    margin-top: 10px;
    padding: 5px;
    border: 1px solid #ccc;
    line-height: 40px;
  }
  .main {
    padding: 0 10px;
    margin-top: 10px;
  }
  .input_style {
    display: inline-block;
    width: 200px;
  }
}
</style>
